﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Diagnostics;

namespace Portal
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class FileUploadER : SPItemEventReceiver
    {
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            try
            {
                SPWeb web = properties.Web;
                web.AllowUnsafeUpdates = true;
                SPList list = web.GetList("/Lists/AllPosts/");
                SPListItem item = list.AddItem();
                item["UserAccount"] = properties.Web.CurrentUser;
                item["DocumentID"] = properties.ListItemId;
                item["PostType"] = "Video";
                item["SimplePost"] = properties.Web.CurrentUser.Name + " Added a Document";
                item.Update();
                web.AllowUnsafeUpdates = false;

                base.ItemAdded(properties);
            }
            catch (Exception ex)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {


                    string cs = "Portal";
                    EventLog elog = new EventLog();

                    if (!EventLog.SourceExists(cs))
                    {
                        EventLog.CreateEventSource(cs, cs);
                    }
                    elog.Source = cs;
                    elog.EnableRaisingEvents = true;
                    elog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
                    elog.Close();

                });
            }
        }


    }
}